package com.sig.webservice;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.sig.models.Structures;

import android.util.Log;

public class ParseAllName extends DefaultHandler{
	
	public static final String BALISE_STRUCTURES="structures";
	public static final String BALISE_NAME="name";
	
	private Structures structure;
	private List<String> name=new ArrayList<String>();
	private boolean inName=false;
	
	public void startElement(String uri, String localName,
			String qName, Attributes attributes) throws SAXException{
		if(qName.equals(BALISE_STRUCTURES)){
			structure=new Structures();
			Log.d("STUCT", "in balise structure");
		}else if(qName.equals(BALISE_NAME)){
			inName=true;
			
		}
		
	}
	
	public void endElement(String uri, String localName, String qName)
	throws SAXException{
		
		if(qName.equals(BALISE_STRUCTURES)){
			inName=false;
			structure.setName(name);
		}else if(qName.equals(BALISE_NAME)){
			
		}
	}
	
	public void characters(char ch[], int start, int length) throws SAXException {
		String value = new String(ch, start, length);
		
		if(inName){
			name.add(value);
		}
	}
	
	/**
	 * Load the file 
	 */
	public void load(File in,ParseAllName parse) {
		SAXParserFactory saxFactory=SAXParserFactory.newInstance();
		try {
			SAXParser parser=saxFactory.newSAXParser();			
			parser.parse(in, parse);
			Log.d("FILE", "Read XML");
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			Log.d("FILE", " NOT found");
			e.printStackTrace();
		}
	}

	/**
	 * @return the structure
	 */
	public Structures getStructure() {
		return structure;
	}
	

}
